package com.apps.ftapp.activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.IdRes;

import com.apps.ftapp.R;
import com.apps.ftapp.adapter.MyErrorSubjectAdapter;
import com.apps.ftapp.adapter.MyErrorSubjectRecAdapter;
import com.apps.ftapp.model.MyErrorModel;
import com.apps.ftapp.model.RecSubjectModel;
import com.apps.ftapp.util.ActivitySkipUtil;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.StatusBarUtils;
import com.apps.ftapp.util.Tools;
import com.google.gson.Gson;
import com.jaeger.library.StatusBarUtil;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cz.msebera.android.httpclient.Header;
import info.hoang8f.android.segmented.SegmentedGroup;

public class MyErrorSubjectNewActivity extends Activity {
    @BindView(R.id.listView)
    ListView listView1;
    @BindView(R.id.listView2)
    ListView listView2;
    @BindView(R.id.wodecuoti)
    RadioButton wodecuoti;
    @BindView(R.id.tuijiancuoti)
    RadioButton tuijiancuoti;
    @BindView(R.id.segmented2)
    SegmentedGroup segmented2;
    @BindView(R.id.backBtn)
    ImageView backBtn;

    private Gson gson = new Gson();
    private List<MyErrorModel.MyTestpaperResultsBean> dataList1 = new ArrayList<>();
    private MyErrorSubjectAdapter myErrorSubjectAdapter;

    private List<RecSubjectModel.DataBean> dataList2 = new ArrayList();
    private MyErrorSubjectRecAdapter myErrorSubjectRecAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_error_subject_new);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(MyErrorSubjectNewActivity.this, Color.WHITE, 1);
        StatusBarUtils.StatusBarLightMode(this);
        init();
    }

    private void init() {
        segmented2.setTintColor(getResources().getColor(R.color.index_zhenti_btn_color));

        wodecuoti.setChecked(true);

        myErrorSubjectAdapter = new MyErrorSubjectAdapter(getApplicationContext(), dataList1);
        listView1.setAdapter(myErrorSubjectAdapter);

        myErrorSubjectRecAdapter = new MyErrorSubjectRecAdapter(getApplicationContext(), dataList2);
        listView2.setAdapter(myErrorSubjectRecAdapter);

        listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                HashMap map = new HashMap();
                map.put("id", dataList1.get(position).getId());

                L.i("main", ":::::" + dataList1.get(position).getId());

//                ActivitySkipUtil.skipAnotherActivity(MyErrorSubjectActivity.this, MyErrorDetailActivity.class, map);

                ActivitySkipUtil.skipAnotherActivity(MyErrorSubjectNewActivity.this, StErrorJxActivity.class, map);

            }
        });
        listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HashMap map = new HashMap();
                map.put("id", dataList2.get(position).getQuestionId());
//
//                L.i("main",":::::"+dataList.get(position).getQuestionId());
//

//                ActivitySkipUtil.skipAnotherActivity(MyErrorSubjectRecActivity.this, StErrorJxActivity.class, map);


                ActivitySkipUtil.skipAnotherActivity(MyErrorSubjectNewActivity.this, MyErrorDetailActivity.class, map);
            }
        });

        segmented2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                switch (i) {
                    case R.id.wodecuoti:
                        //wodecuoti.setChecked(true);
                        listView1.setVisibility(View.VISIBLE);
                        listView2.setVisibility(View.GONE);
                        break;

                    case R.id.tuijiancuoti:
                        listView1.setVisibility(View.GONE);
                        listView2.setVisibility(View.VISIBLE);
                        break;
                }
            }
        });

        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        loadmyerror();

    }


    private void loadmyerror() {
        RequestParams params = new RequestParams();
        params.put("token", Constant.token);
        params.put("userId", Constant.uid);
        L.i("main", "token:::" + Constant.token + ":::" + Constant.uid);
        Async.post("my/quiz", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", new String(responseBody));
//                    List<MyErrorModel> result = gson.fromJson(new String(responseBody), new TypeToken<List<MyErrorModel>>() {
//                    }.getType());//把JSON格式的字符串转为List
                    MyErrorModel jsonBean = gson.fromJson(new String(responseBody), MyErrorModel.class);
                    if (Tools.isIntNull(jsonBean.getSuccess()) == 1) {
                        dataList1.addAll(jsonBean.getMyTestpaperResults());
                        myErrorSubjectAdapter.addRes(jsonBean.getMyTestpaperResults());

                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }

            @Override
            public void onFinish() {
                loaderrrem();
                super.onFinish();
            }
        });

    }

    private void loaderrrem() {
        RequestParams params = new RequestParams();
        params.put("token", Constant.token);
        params.put("userId", Constant.uid);
        Async.post("top/quiz", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", new String(responseBody));
                    RecSubjectModel jsonBean = gson.fromJson(new String(responseBody), RecSubjectModel.class);
                    if (Tools.isIntNull(jsonBean.getSuccess()) == 1) {
                        dataList2.addAll(jsonBean.getData());
                        myErrorSubjectRecAdapter.addRes(jsonBean.getData());

                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });
    }


}
